﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace QL_Truong_Hoc
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        public bool LoginSuccess { get; set; }

        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, System.EventArgs e)
        {
            this.BackgroundImageLayout = ImageLayout.None;
            txtUsername.Focus();
            this.LoginSuccess = false;
        }

        private void btnLogin_Click(object sender, System.EventArgs e)
        {
            try
            {
                string strUsername;
                string strPassword;
                string strWhere;
                List<DTO.TNhomQuyen_QuyenDTO> lstRolesDetail;
                DTO.MQuyenDTO objSingleRole;

                strUsername = txtUsername.Text.Trim();
                strPassword = txtPassword.Text.Trim();
                strPassword = clsCommon.fncGetSHA1HashData(strPassword);
                strWhere = string.Format("Username = '{0}' and Password = '{1}'", clsCommon.fncCnvToDbString(strUsername), clsCommon.fncCnvToDbString(strPassword));

                List<DTO.TblUserDTO> lstUser = DAO.TblUserDAO.SelectWhere(Program.gobjDb, strWhere);

                if (lstUser == null)
                {
                    clsCommon.fncMsgInfo("Tên đăng nhập hoặc mật khẩu không đúng. Xin hãy thử lại.");
                    txtUsername.Focus();
                    return;
                }

                if (lstUser.Count <= 0 || lstUser.Count >= 2)
                {
                    clsCommon.fncMsgInfo("Tên đăng nhập hoặc mật khẩu không đúng. Xin hãy thử lại.");
                    txtUsername.Focus();
                }
                else
                {
                    this.LoginSuccess = true;
                    Program.gstrCurrentUser = strUsername;
                    Program.gblnLoggedIn = true;

                    //lay list role
                    #region Get Role List
                    Program.glstRoles.Clear();
                    lstRolesDetail = DAO.TNhomQuyen_QuyenDAO.SelectWhere(Program.gobjDb, string.Format("RoleID = {0}", lstUser[0].RoleID));
                    for (int i = 0; i <= lstRolesDetail.Count - 1; i++)
                    { 
                        //neu ko co quyen thi next
                        if (!lstRolesDetail[i].RoleAllow) continue;

                        //neu co thi cho vao danh sach cac quyen duoc thuc thi
                        objSingleRole = DAO.MQuyenDAO.Select(Program.gobjDb, lstRolesDetail[i].QU_ID);
                        Program.glstRoles.Add(objSingleRole.Qu_code);
                    }
                    #endregion

                    this.Close();
                }
            }
            catch (Exception ex)
            {
                //clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); 
                clsCommon.fncMsgError("Không thể kết nối tới cơ sở dữ liệu.");
            }
        }

        private void lnkConnection_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                using (frmConnectionSetting frmSetting = new frmConnectionSetting())
                {
                    frmSetting.ShowDialog();

                    Program.gobjConn.Close();
                    Program.gobjDb = EnterpriseLibraryContainer.Current.GetInstance<Database>("QL_Truong_Hoc.Properties.Settings.QLTHConnectionString");
                    Program.gobjConn = Program.gobjDb.CreateConnection();

                    try
                    {
                        Program.gobjConn.Open();
                    }
                    catch
                    {
                        clsCommon.fncMsgError("Không thể kết nối tới cơ sở dữ liệu. Xin hãy kiểm tra lại");
                        lnkConnection.Focus();
                        return;
                    }
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}